<template>
    <div>
      <el-form inline :model="queryData" ref="queryData" class="query-form">
        <el-form-item label="UID:" prop="id">
          <el-input type="number" v-model="queryData.id" placeholder="请输入" style="width: 200px" clearable
                    @clear="formClear('id')"/>
        </el-form-item>
        <el-form-item label="手机:" prop="phone">
          <el-input type="number" v-model="queryData.phone" placeholder="请输入" style="width: 200px" clearable
                    @clear="formClear('phone')"/>
        </el-form-item>
        <el-form-item label="认证角色:" prop="roleName">
          <el-select v-model="queryData.roleName" placeholder="请选择" clearable @clear="formClear('roleName')"
                     style="width: 200px">
          </el-select>
        </el-form-item>
        <el-form-item label="注册时间:" prop="createTime">
          <el-date-picker v-model="queryData.createTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户状态:" prop="userStatus">
          <el-select v-model="queryData.userStatus" placeholder="请选择" clearable @clear="formClear('userStatus')"
                     style="width: 200px">
            <el-option label="打开" value="1"/>
            <el-option label="关闭" value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查 询</el-button>
          <el-button @click="onReset">重 置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableList" border style="width: 100%;">
        <el-table-column prop="id" label="UID" align="center"/>
        <el-table-column label="头像" align="center">
          <template slot-scope="{row}">
            <el-image style="width: 30px; height: 30px; border-radius: 50%" :src="row.headimgUrl" fit="cover"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="wxPhone" label="用户手机" align="center"/>
        <el-table-column prop="sex" label="用户性别" align="center"/>
        <el-table-column prop="nickName" label="微信昵称" align="center" show-overflow-tooltip/>
        <el-table-column prop="openid" label="WXID" align="center" show-overflow-tooltip/>
        <el-table-column prop="roleName" label="用户角色" align="center"/>
        <el-table-column prop="createTime" label="注册时间" align="center" show-overflow-tooltip/>
        <el-table-column label="用户状态" align="center" width="100">
          <template slot-scope="{row}">
            <el-switch :value="row.userStatus" @change="putawayEvent($event, row, 'userStatus')"
                       :active-value="1" :inactive-value="0"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="经纪人权限" align="center" width="100">
          <template slot-scope="{row,$index}">
            <el-switch :value="row.agentPower" @change="putawayEvent($event, row,'agentPower')"
                       :active-value="1" :inactive-value="0"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="园区权限" align="center" width="100">
          <template slot-scope="{row}">
            <el-switch :value="row.gardenPower" @change="putawayEvent($event, row, 'gardenPower')"
                       :active-value="1" :inactive-value="0"></el-switch>
          </template>
        </el-table-column>
      </el-table>
      <Paging v-model="queryData" @change="getRequest"></Paging>
    </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    name: 'userList',
    data() {
      return {
        tableList: [],
        queryData: {
          id: null, //用户id
          createTime: null, //注册时间
          phone: null, //手机号
          roleName: null, //用户角色名字
          userStatus: null, // 用户状态 0 关闭 1 打开
          pageNum: 1,
          pageSize: 10,
          total: 0
        }
      }
    },
    beforeMount() {
      this.getRequest()
    },
    methods: {
      /**
       *  开关事件
       */
      async putawayEvent(event, row,key) {
        this.$confirm('确认修改该条数据吗?', '提示', this.confirm.options).then(async() => {
          await this.$ajax.post('/wx/user/backstage/update', { id: row.id, [key]: event}).finally(() => this.getRequest())
          this.$message.success('操作成功')
        })
      },
      /**
       * 重置事件
       */
      onReset() {
        this.queryData.pageNum = 1
        this.queryData.province = null
        this.queryData.city = null
        this.queryData.county = null
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const params = JSON.parse(JSON.stringify(this.queryData))
        const res = await this.$ajax.get('/wx/user/backstage/list', { params })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      /**
       * form-input 置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
/deep/ input[type='number'] {
  -moz-appearance: textfield !important;
  }
</style>
